package com.witker.common.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.witker.common.DataPage;
import com.witker.common.util.ClassReflectUtil;
import com.witker.common.util.NoneTypeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * 模块业务控制器的父类
 * Created by BOCAI on 2018/9/15 20:32.
 */
public abstract class ModuleServiceSupport<M extends MapperSupport<T>, T> extends ServiceImpl<M, T>  implements  ModuleService<T> {

    /**
     * 日志记录器
     */
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public T bean(T bean) {
        T result = null;
        QueryWrapper<T> queryWrapper = new QueryWrapper<>(bean);
        result = this.getOne(queryWrapper);
        return result;
    }

    @Override
    public List<T> beans(T bean) {
        List<T> result = null;
        QueryWrapper<T> queryWrapper = new QueryWrapper<>(bean);
       result = this.list(queryWrapper);
        return result;
    }

    @Override
    public List<T> beans(T bean, DataPage dataPage, boolean isAsc, String... orderBy) {
        List<T> result = null;
        QueryWrapper<T> queryWrapper = new QueryWrapper<>(bean);
        queryWrapper.orderBy(orderBy[0] != null, isAsc, orderBy);
        IPage<T> page = new Page<>(dataPage.getIndex(), dataPage.getSize());
        page = this.page(page,queryWrapper);
        dataPage.setMax(page.getPages());
        dataPage.setCount(page.getTotal());
        result = page.getRecords();
        return result;
    }
}
